package com.longma.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.SysUser;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.*;
import com.longma.server.service.impl.SysUserServiceImpl;
import com.longma.server.util.AuthContextUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */

@RestController
@Validated
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl userService;


    @ApiOperation(value = "增加用户")
    @PostMapping("/add")
    public UserResponseEntity<SysUser> addUser(@Valid @RequestBody SysUserAddDTO addRequestDTO){
        return UserResponseEntity.success(userService.addUser(addRequestDTO));
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public UserResponseEntity<Boolean> deleteUser(@NotBlank(message = "用户id不能为空") @RequestParam Integer id){
        return UserResponseEntity.success(userService.deleteUser(id));
    }

    @ApiOperation(value = "删除用户(批量)")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteUserByIds(@RequestParam @Size(min = 1,message = "用户列表不能为空") List<@NotBlank(message = "用户id不能为空")  Integer> ids){
        return UserResponseEntity.success(userService.deleteUserByIds(ids));
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    public UserResponseEntity<SysUser> updateUser(@Valid  @RequestBody SysUserUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(userService.updateUser(updateRequestDTO));
    }

    @ApiOperation(value = "修改登录用户信息")
    @PostMapping("/updateme")
    public UserResponseEntity<SysUser> updateMe(@Valid @RequestBody SysUserSelfUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(userService.updateMe(updateRequestDTO));
    }

    @ApiOperation(value = "修改登录用户密码")
    @PostMapping("/changepassword")
    public UserResponseEntity<SysUser> changePassword(@Valid @RequestBody SysUserPasswordUpdateDTO passwordDTO){
        return UserResponseEntity.success(userService.changePassword(passwordDTO));
    }

    @ApiOperation(value = "检测登录名是否已存在")
    @GetMapping("/checkusername")
    public UserResponseEntity<Boolean> checkUsername(@NotBlank(message = "用户名不能为空" )@RequestParam String username){
        return UserResponseEntity.success(userService.checkUsername(username));
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/getme")
    public UserResponseEntity<SysUser> getMe(){
        return UserResponseEntity.success(userService.getUser(AuthContextUtil.getLoginName()));
    }

    @ApiOperation(value = "获取所有用户(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<SysUser>> getByPage(@ApiParam(value = "分页查询，分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(userService.getByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }

}

